package cn.jbolt.core.util;

public class JBoltFileSizeUtil {
    public static String format(Long fileSize) {
        if(fileSize==null || fileSize<=0){
            return "0 B";
        }
        if (fileSize < 1024) {
            return fileSize + " B";
        } else if (fileSize < 1024 * 1024) {
            return String.format("%.2f KB", (double) fileSize / 1024);
        } else if (fileSize < 1024 * 1024 * 1024) {
            return String.format("%.2f MB", (double) fileSize / (1024 * 1024));
        } else if (fileSize < 1024 * 1024 * 1024 * 1024) {
            return String.format("%.2f GB", (double) fileSize / (1024 * 1024 * 1024));
        } else {
            return String.format("%.2f TB", (double) fileSize / (1024 * 1024 * 1024 * 1024));
        }
    }
}
